<template>
	<view class="login-container">
		<view class="logo-area">
			<image class="logo" src="/static/logo.png" mode="aspectFit"></image>
			<text class="app-name">叮咚吉派-家政阿姨</text>
		</view>

		<view class="login-area">
			<button v-if="!isLoggedIn && !isLoading && isAgree " class="login-btn" open-type="getPhoneNumber"
				@getphonenumber="getPhoneNumber">
				手机号快捷登录
			</button>
			<button v-if="!isLoggedIn && !isLoading && !isAgree " class="login-btn" @click="handleLoginClick">
				手机号快捷登录
			</button>
			<view style="margin-top: 30rpx;">
				<button class="cancel-btn" @click="cancelLogin">取消登录</button>
			</view>


			<view v-if="isLoading" class="loading">
				<view class="loading-icon"></view>
				<text class="loading-text">登录中...</text>
			</view>
			<view class="footer">
				<checkbox :checked="isAgree" @click="isAgree =!isAgree"></checkbox>
				<text class="agreement-text">已阅读并同意</text>
				<text class="agreement-link" @tap="showUserAgreement">《用户协议》</text>
				<text class="agreement-text">和</text>
				<text class="agreement-link" @tap="showPrivacyPolicy">《隐私政策》</text>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				isLoggedIn: false,
				isLoading: false,
				inviterCode: '',
				isAgree: false
			}
		},

		onLoad(options) {
			this.inviterCode = options.inviterCode || '';
		},

		methods: {
			//检查是否绑定手机号
			handleLoginClick() {
				if (!this.isAgree) {
					this.showToast('请先阅读并同意用户协议和隐私政策');
				}
			},
			getPhoneNumber(e) {
				this.bindPhone(e.detail.code)
			},


			// 发送登录请求
			bindPhone(phoneCode) {
				try {
					const requestData = {
						phoneCode: phoneCode
					};

					this.$Request.postFormHost(this.$Api.bindWxPhone, requestData)
						.then(
							res => {
								if (res.status == 0) {
									uni.navigateBack();
								} else {
									uni.showToast({
										icon: "none",
										title: res.msg
									})
								}
							});
				} catch (error) {
					throw error;
				}
			},

			// 处理登录错误
			handleLoginError(error) {
				// 处理用户取消授权的情况
				if (error.errMsg && error.errMsg.indexOf('cancel') > -1) {
					this.showToast('授权已取消');
				} else {
					const errorMsg = error.message || '登录失败，请稍后再试';
					this.showToast(errorMsg);
					console.error('登录失败:', error);
				}
			},

			// 跳转到服务大厅
			navigateToServiceHall() {
				uni.switchTab({
					url: '/pages/customer/serviceHall'
				});
			},
			// 跳转到需求大厅
			navigateToDemandHall() {
				uni.switchTab({
					url: '/pages/index/index'
				});
			},

			// 显示toast消息
			showToast(title, icon = 'none') {
				uni.showToast({
					title,
					icon,
					duration: 2000
				});
			},

			// 显示用户协议
			showUserAgreement() {
				uni.navigateTo({
					url: '/pages/agreement/user'
				});
			},

			// 显示隐私政策
			showPrivacyPolicy() {
				uni.navigateTo({
					url: '/pages/agreement/privacy'
				});
			},
			cancelLogin() {
				uni.navigateBack();
			},

		}
	}
</script>

<style>
	.login-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
		height: 100vh;
		background: linear-gradient(#FFF8F0 -1%, #FFEAD7 50%, #FFD0A8 100%);
		padding: 60rpx 40rpx;
		box-sizing: border-box;
	}

	.logo-area {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-top: 120rpx;
	}

	.logo {
		width: 200rpx;
		height: 200rpx;
		margin-bottom: 40rpx;
		border-radius: 20rpx;
		box-shadow: 0 10rpx 30rpx rgba(217, 142, 86, 0.2);
	}

	.app-name {
		font-size: 44rpx;
		font-weight: bold;
		color: #8B4513;
		margin-bottom: 20rpx;
		text-shadow: 0 2rpx 4rpx rgba(139, 69, 19, 0.1);
	}

	.login-area {
		width: 100%;
		margin: 120rpx 0;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.login-btn {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width: 85%;
		height: 90rpx;
		background: linear-gradient(to right, #FF8C3D, #FF6A3D);
		color: #FFFFFF;
		font-size: 34rpx;
		font-weight: 500;
		border-radius: 45rpx;
		margin: 0 auto;
		box-shadow: 0 8rpx 24rpx rgba(255, 138, 61, 0.35);
		transition: all 0.3s ease;
	}

	.login-btn:active {
		transform: scale(0.98);
		box-shadow: 0 4rpx 12rpx rgba(255, 138, 61, 0.3);
	}

	.wechat-icon {
		width: 44rpx;
		height: 44rpx;
		margin-right: 12rpx;
	}

	.loading {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		height: 120rpx;
	}

	.loading-icon {
		width: 60rpx;
		height: 60rpx;
		border: 6rpx solid #FFF0E6;
		border-top: 6rpx solid #FF8C3D;
		border-radius: 50%;
		margin-bottom: 20rpx;
		animation: spin 1s linear infinite;
	}

	.loading-text {
		font-size: 28rpx;
		color: #B26B3A;
	}

	.cancel-btn {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;

		width: 35vw;
		height: 70rpx;
		background-color: #9f9f9f;
		color: #FFFFFF;
		font-size: 34rpx;
		border-radius: 35rpx;
		margin: 0 auto;
		transition: all 0.3s ease;
	}

	@keyframes spin {
		0% {
			transform: rotate(0deg);
		}

		100% {
			transform: rotate(360deg);
		}
	}

	.footer {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #73553f;
		margin-top: 30rpx;
		margin-bottom: 50rpx;
		line-height: 1.5;
		flex-wrap: wrap;
	}

	.agreement-text {
		color: #A67B5B;
	}

	.agreement-link {
		color: #E86A10;
		margin: 0 6rpx;
		font-weight: 500;
	}

	.getPhone-btn {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width: 50vw;
		height: 70rpx;
		background-color: #ff5500;
		color: #ffffff;
		font-size: 34rpx;
		border-radius: 35rpx;
		margin: 20rpx auto;
		box-shadow: 0 8rpx 24rpx rgba(255, 138, 61, 0.35);
		transition: all 0.3s ease;
	}
</style>